package com.tengfei.gulishop.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * @author feige
 * @version v1.0
 * @date 2022-07-14-9:25
 * @description 网关跨域同一配置
 */

@Configuration
public class CorsConfig {

    @Bean
    public CorsWebFilter corsFilter(){
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        // 配置跨域
        // 任意请求头
        corsConfiguration.addAllowedHeader("*");
        // 任意方法
        corsConfiguration.addAllowedMethod("*");
        // 任意请求来源
        corsConfiguration.addAllowedOrigin("*");
        // 允许请求携带cookie
        corsConfiguration.setAllowCredentials(true);
        source.registerCorsConfiguration("/**",corsConfiguration);
        return new CorsWebFilter(source);
    }

}
